<!--
 *首页
 *带处理：面包屑处理
 * @Author: 舒克
 * @Official Accounts: 氧化1028
 * @email: 143587623@qq.com
 * @github: https://gitee.com/KingSio
 * @Date: 2020-12-10 18:59:05
 * @LastEditTime: 2021-04-06 10:59:26
 * @FilePath: \小程序d:\课堂\课堂\个人\Vue\哇麦游戏后台\vue\1.vue全站管理系统\vue_shop\src\components\Home.vue
-->
<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/img/cms.png"
          alt="123"
          :style="{ width: '50px', 'border-radius': '50%','margin-left':'15px'}"
        />
        <span>氧化科学后台</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域-->
    <el-container>
      <!-- 侧边栏区域 -->
      <!--siCollapse-如果是true侧边栏变小，如果为false就变大  -->
      <el-aside :width="siCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          |||
        </div>
        <!-- 侧边栏菜单区域 unique-opened只保持一个子菜单的展开
        collapse-transition关闭动画
        router开启路由模式
        default-active高亮，点击变色
        -->
        <el-menu
          background-color="#373D41"
          text-color="#fff"
          active-text-color="#409BFF"
          :unique-opened="true"
          :collapse="siCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- index这里只接受字符串 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 
            saveNavState()方法，点击保存这个效果在本地，刷新依然保存当前的效果
            -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main>
        <!-- 这个判断有问题，所以子组件拿不到，后面要改 -->
        <el-card class="box-card" v-if="thisPath">
          <CrumbsTag @tag="getChildTag"></CrumbsTag>
        </el-card>
        <!-- 面包屑导航 -->

        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { home } from '@/api/home'

import CrumbsTag from '@/components/common/CrumbsTag.vue'
import Storage from '@/utils/Storage.js'
export default {
  components: {
    CrumbsTag
  },

  data() {
    return {
      //左侧菜单数据
      menulist: [],
      iconsObj: {
        //这个数字表示每一个菜单的id唯一值
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      siCollapse: false, //默认不收起侧边栏
      activePath: '', //被激活的链接地址，保存在本地
      thisPath: true,
      sessionNewTagNav: [],

    }
  },
  created() {
        //完成data数据初始化，判断当前是在那个路由
      if(this.$route.name=="首页"){
        this.thisPath=false
      }
 
    this.getMenuList() //获取侧边栏栏数据
    this.activePath = window.sessionStorage.getItem('activePath') //获取点击后的状态
    //获取头部的面包屑导航的会话数据
 
  },
  mounted() {
  },

  watch: {
    //监听路由的变化，"$router"有两个参数to,from
    $route(to, from) {
   
    
      //首页不用添加面包屑
      if (this.$route.path == '/login') {
        console.log('这个页面是不用添加面包屑')
      } else {
        /* 
         path：当前的路由
         prePath:上一个的路由
         preQuery：上一个的路由对象
        */
        this.$store.commit('ADDTAG', {
          path: to.path,
          name: to.name,
          //标志该页面是否缓存
          cache: to.path.split('/')[0],
          query: to.query,
          prePath: from.path,
          preQuery: from.query
        })
        this.$nextTick(() => {
          console.log(this.$store.state.TagList.tagName)
          let isRepeat = this.$store.state.TagList.tagName.find(v => {
            if (to.path.split('/')[1] === v) {
              console.log(isRepeat)
              return true
            }
          })
            if (isRepeat) {
            return
          } else {
            if (to.path.split('/')[3]) {
              this.$store.state.TagList.tagName.push(to.path.split('/')[1])
            }
          }
        })
      }
    }
  },
  methods: {
    //退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //侧边栏收起
    toggleCollapse() {
      this.siCollapse = !this.siCollapse
    },
    //获取菜单栏信息，左侧的
    async getMenuList() {
      //  let data=await this.$http.get("menus");不使用解构
      //   console.log(data.data)
      //   console.log(data.data.data)
      //let { data: res } = await this.$http.get('menus')没有封装api之前
      const { data: res } = await home()
      if (res.meta.status == 200) {
        this.menulist = res.data
      } else {
        return this.$message.error(res.meta.msg)
      }
    },
    //点击存放点击的效果，并且进行切换activePath:存放路由地址
    saveNavState(activePath) {
      //保存在当前会话：保存的是当前已经激活的链接地址（就是二级菜单，颜色也存放好了）
      window.sessionStorage.setItem('activePath', activePath)
      console.log(activePath) //  输出地址：/users
      this.activePath = activePath
      this.thisPath=true;//切换的是否的显示面包屑
       
     
    },
    //点击面包屑，同步到侧边栏跟着变动，子组件传过来的
    getChildTag(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #373d41;
  .el-menu {
    border: none; //解决侧边栏不对齐的bug
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  text-align: center;
  color: white;
  line-height: 24px;
  font-size: 10px;
  letter-spacing: 0.2em;
  cursor: pointer; //鼠标放在这个图标变成小手
}
.box-card {
  height: 40px;
  margin-bottom: 10px;
}
</style>
